package com.pig4cloud.pig.djjk.schedule;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pig4cloud.pig.admin.api.entity.SysDept;
import com.pig4cloud.pig.admin.service.SysDeptService;
import com.pig4cloud.pig.djjk.entity.DeviceEntity;
import com.pig4cloud.pig.djjk.service.DeviceService;
import com.pig4cloud.pig.djjk.service.HikService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component("hikSchedule")
@AllArgsConstructor
public class HikSchedule {
	@Autowired
	HikService hikService;
	private final SysDeptService deptService;
	@Autowired
	DeviceService deviceService;

	public String syncRegion(String parms) {
		log.info("开始同步区域");
		int pageNo = 1;
		int pageSize = 200;
		JSONArray arr = new JSONArray();
		JSONObject data = hikService.regionsSearch(pageNo, pageSize);
		if(data!=null){
			arr.addAll(data.getJSONArray("list"));
//			int total = data.getInteger("total");
//			int pageCount = (int) Math.ceil(total / pageSize);
//			for (int i = 2; i <= pageCount; i++) {
//				JSONObject dataJson = hikService.regionsSearch(i, pageSize);
//				if(dataJson!=null) {
//					arr.addAll(dataJson.getJSONArray("list"));
//				}
//			}
		}

		if(CollUtil.isNotEmpty(arr)){
			Map<String,Long> map = new HashMap<>();
			for (int i = 0; i < arr.size(); i++) {
				JSONObject item = arr.getJSONObject(i);
				String parentIndexCode = item.getString("parentIndexCode");
				String indexCode = item.getString("indexCode");
				SysDept sysDept = deptService.selectByThirdId(indexCode);
				if(sysDept !=null){
					sysDept.setName(item.getString("name"));
					SysDept parent = deptService.selectByThirdId(parentIndexCode);
					if(parent!=null && sysDept.getParentId()!=parent.getDeptId()){
						sysDept.setParentId(parent.getDeptId());
					}
					sysDept.setUpdateBy("system");
					deptService.updateById(sysDept);
					map.put(indexCode,sysDept.getDeptId());
				}else{
					SysDept dept = new SysDept();
					dept.setName(item.getString("name"));
					if(map.containsKey(parentIndexCode)){
						dept.setParentId(map.get(parentIndexCode));
					}else if(StrUtil.equals(parentIndexCode,"-1")){
						dept.setParentId(0l);
					}
					dept.setThirdId(indexCode);
					dept.setCreateBy("system");
					dept.setUpdateBy("system");
					dept.setSortOrder(i);
					deptService.save(dept);
					map.put(indexCode,dept.getDeptId());
				}
			}
		}
		log.info("同步区域完成,{}",arr.size());
		syncCameras();
		return "success";
	}

	public String syncCameras() {
		log.info("开始同步摄像头");
		int pageNo = 1;
		int pageSize = 5000;
		JSONArray arr = new JSONArray();
		JSONObject data = hikService.cameraSearch(pageNo, pageSize);
		if (data != null) {
			arr.addAll(data.getJSONArray("list"));
//			int total = data.getInteger("total");
//			int pageCount = (int) Math.ceil(total / pageSize);
//			for (int i = 2; i <= pageCount; i++) {
//				JSONObject dataJson = hikService.regionsSearch(i, pageSize);
//				if (dataJson != null) {
//					arr.addAll(dataJson.getJSONArray("list"));
//				}
//			}
		}

		if (CollUtil.isNotEmpty(arr)) {
			for (int i = 0; i < arr.size(); i++) {
				JSONObject item = arr.getJSONObject(i);
				String unitIndexCode = item.getString("unitIndexCode");
				String cameraIndexCode = item.getString("cameraIndexCode");
				String latitude = item.getString("latitude");
				String longitude = item.getString("longitude");

				DeviceEntity device = deviceService.getDeviceByCode(cameraIndexCode);
				if (device != null) {
					device.setDeviceName(item.getString("name"));
					SysDept region = deptService.selectByThirdId(unitIndexCode);
					if (region != null) {
						device.setDeptId(region.getDeptId());
					}
					device.setLatitude(latitude);
//					device.setDeviceCode(cameraIndexCode);
					device.setLongitude(longitude);
					device.setStatus(item.getInteger("status"));
					deviceService.updateById(device);
				} else {
					device = new DeviceEntity();
					device.setDeviceName(item.getString("name"));
					SysDept region = deptService.selectByThirdId(unitIndexCode);
					if (region != null) {
						device.setDeptId(region.getDeptId());
					}
					device.setDeviceCode(cameraIndexCode);
					device.setLatitude(latitude);
					device.setLongitude(longitude);
					device.setDevicePosition("1");
					device.setStatus(item.getInteger("status"));
					deviceService.save(device);
				}
			}
		}
		log.info("同步摄像头完成,{}",arr.size());
		return "success";
	}
}
